// 引入async模块
const async = require('async');
// 引入常量模块
const Constant = require('./constant');

const exportObj = {
    clone,
    checkParams,
    autoFn,
    numChange
};

module.exports = exportObj;

function clone(obj) {
    return JSON.parse(JSON.stringify(obj));
}

function checkParams(params, checkArr, cb) {
    let flag = true;
    checkArr.forEach(v => {
        if (!params[v]) {
            flag = false;
        }
    });
    if (flag) {
        cb(null);
    } else {
        cb(Constant.LACK);
    }

}

function autoFn(tasks, res, resObj) {
    async.auto(tasks, function(err) {
        if (!!err) {
            console.log(JSON.stringify(err));
            res.json({
                code: err.code || Constant.DEFAULT_ERROR.code,
                msg: err.msg || JSON.stringify(err)
            });
        } else {
            res.json(resObj);
        }
    });
}


function numChange(num) {
    if (num >= 10000) {
        var n1 = Math.floor(num / 10000)
        var n2 = Math.floor((num % 10000) / 1000)
        if (n2 == 0) {
            return n1 + '万'
        } else {
            return n1 + '.' + n2 + '万'
        }
    } else {
        return num
    }
}